﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cspl.Calculator.Lib.Interface;

namespace Cspl.Calculator.Lib
{
    public class BasicCalculator : IBasicCalculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }

        public int Mul(int a, int b)
        {
            return a * b;
        }

        public int Div(int a, int b)
        {
            int retval;

            try
            {
                retval = a / b;
            }
            catch (Exception ex)
            {
                Console.WriteLine("An exeption occured : " + ex.Message);
                retval = 0;
            }
            return retval;

        }

        public int Sub(int a, int b)
        {
            return a - b;
        }


        //public int AddList(IEnumerable<int> listofnumber)
        //{
         //   int retval = 0;
        
//            foreach (int number in listofnumber)
  //          {
    //            retval += number;
      //      }
        //    return retval;
       // }

        public int ComplexListOperation(IEnumerable<int> listofnumber, CalOperation calop)
        {
            int retval = 0;

            foreach (int number in listofnumber)
            {
                switch (calop)
                { 
                    case CalOperation.Add:
                        retval = this.Add(retval, number);
                        break;
                    case CalOperation.Mul:
                        retval = this.Mul(retval, number);
                        break;
                    case CalOperation.Div:
                        retval = this.Div(retval, number);
                        break;
                    case CalOperation.Sub:
                        retval = this.Sub(retval, number);
                        break;
                    default:
                        break;
                }
            } return retval;
        }
    }
}
